package com.zhm.demo.selenium;

import io.github.bonigarcia.wdm.WebDriverManager;
import org.apache.commons.io.FileUtils;
import org.openqa.selenium.By;
import org.openqa.selenium.OutputType;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.chrome.ChromeOptions;

import java.io.File;
import java.io.IOException;
import java.net.MalformedURLException;
import java.time.Duration;
import java.util.HashMap;
import java.util.function.Consumer;

/**
 * SimpleSelenium
 *
 * @apiNote <a href="https://www.selenium.dev/zh-cn/documentation/overview/">文档详情</a>
 * @since 1.0.0
 */

public class SimpleSelenium {

    public final WebDriver driver;

    public SimpleSelenium() {
//        WebDriverManager.chromedriver().remoteAddress("http://localhost:4444/wd/hub").setup();
        WebDriverManager.chromedriver().setup();

        HashMap<String, Object> prefs = new HashMap<>();
        prefs.put("download.default_directory", "d:\\Downloads"); // 替换为实际路径
        prefs.put("download.prompt_for_download", false); // 禁止下载弹窗
        prefs.put("safebrowsing.enabled", true); // 启用安全浏览（可选）

        ChromeOptions options = new ChromeOptions();
        options.setExperimentalOption("prefs", prefs);
        // 初始化WebDriver
        this.driver = new ChromeDriver(options);
    }

    public SimpleSelenium(WebDriver driver) {
        this.driver = driver;
    }

    public SimpleSelenium openWeb() {
        driver.get("https://www.selenium.dev/selenium/web/web-form.html");
        return this;
    }

    public SimpleSelenium getWebTitle(Consumer<? super String> action) {
        action.accept(driver.getTitle());
        return this;
    }

    public SimpleSelenium testFillForm() {
        WebElement textBox = driver.findElement(By.name("my-text"));
        // 设置textBox的值为“Test selenium”
        textBox.clear();
        textBox.sendKeys("Test selenium");
        return this;
    }

    public SimpleSelenium testSubmit() {
        WebElement submitButton = driver.findElement(By.cssSelector("button"));
        submitButton.click();
        return this;
    }

    public SimpleSelenium testGetMessage(Consumer<? super String> action) {
        WebElement message = driver.findElement(By.id("message"));
        action.accept(message.getText());
        return this;
    }

    public SimpleSelenium testScreen() throws IOException {
        WebElement textBox = driver.findElement(By.id("file-1"));

        File screenshotAs = textBox.getScreenshotAs(OutputType.FILE);
        File destination = new java.io.File("d:\\element_screenshot.png");
        // 将临时截图文件复制到目标路径
        FileUtils.copyFile(screenshotAs, destination);
        return this;
    }

    public SimpleSelenium testDownload() {
        WebElement textBox = driver.findElement(By.id("file-1"));

        textBox.click();

        try {
            Thread.sleep(10000);
        } catch (InterruptedException e) {
            throw new RuntimeException(e);
        }
        return this;
    }

    public void quit() {
        driver.quit();
    }


    public static void main(String[] args) throws IOException {

        var simpleSeleniumTest = new SimpleSelenium();
        // 1. 打开网页
        simpleSeleniumTest.driver.manage().timeouts().implicitlyWait(Duration.ofMillis(500));
        simpleSeleniumTest.driver.get("https://www.selenium.dev/selenium/web/downloads/download.html");

        // 2. 获取网页标题，并断言为Web form
        String title = simpleSeleniumTest.driver.getTitle();
        System.out.println(title);

        simpleSeleniumTest.testDownload();
        simpleSeleniumTest.testScreen();

        simpleSeleniumTest.quit();
    }
}

